﻿namespace Microsoft.VisualStudio.IDE.ToolboxControlsInstaller
{
    using Microsoft.MetadataReader;
    using Microsoft.VisualStudio.OLE.Interop;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;
    using Microsoft.VisualStudio.Toolbox;
    using Microsoft.Win32;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Drawing.Design;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Runtime.Versioning;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading;
    using System.Windows;

    public class ToolboxInstallerPackage : Package, IVsToolboxDataProvider
    {
        private List<RegisteredAssembly> _cachedAssemblies;
        private List<RegisteredAssembly> _registeredAssemblies;
        private static readonly int _silverlightFormat = DataFormats.GetDataFormat("CF_WINFXSILVERLIGHT_TOOL").Id;
        private const string _silverlightFormatString = "CF_WINFXSILVERLIGHT_TOOL";
        private const string DefaultCategory = "General";
        private const string DefaultWPFOrSilverlightCategory = "All Controls";
        private static SHA512 hashAlgorithm = SHA512.Create();
        private const string RegKey_Toolbox = "Toolbox";
        private const string ToolboxControlsInstaller = "ToolboxControlsInstaller";
        private const string ToolboxControlsInstallerCache = "ToolboxControlsInstallerCache";
        private const string ValueName_RegistryHash = "RegistryHash";

        public ToolboxInstallerPackage()
        {
            base.ToolboxInitialized += new EventHandler(this.PackageToolbox_ToolboxInitialized);
            base.ToolboxUpgraded += new EventHandler(this.PackageToolbox_ToolboxUpgraded);
        }

        private List<RegisteredAssembly> AddToolboxItems(List<RegisteredAssembly> assembliesToAddToToolbox)
        {
            if (assembliesToAddToToolbox.Count == 0)
            {
                return new List<RegisteredAssembly>();
            }
            List<RegisteredAssembly> list = new List<RegisteredAssembly>();
            IVsActivityLog log = base.GetService(typeof(SVsActivityLog)) as IVsActivityLog;
            log.LogEntry(3, this.ToString(), "Toolbox Controls Installer Package attempting to add Toolbox items");
            IToolboxService service = (IToolboxService) base.GetService(typeof(IToolboxService));
            ToolboxService.UnloadToolboxItems();
            XamlAssembly.UnloadAppDomain();
            List<string> list2 = new List<string>();
            List<string> list3 = new List<string>();
            foreach (RegisteredAssembly assembly in assembliesToAddToToolbox)
            {
                bool flag = false;
                try
                {
                    if (assembly is XamlAssembly)
                    {
                        try
                        {
                            if (list3.Contains(assembly.AssemblyName.Name))
                            {
                                XamlAssembly.UnloadAppDomain();
                                list3.Clear();
                            }
                            list3.Add(assembly.AssemblyName.Name);
                            flag = assembly.AddItemsToToolbox(this, this.ApplicationRegistryRoot);
                            goto Label_03FF;
                        }
                        catch (InvalidOperationException)
                        {
                            log.LogEntry(2, this.ToString(), string.Format("Error adding toolbox items from assembly {0}.  WPF toolbox items might not be supported by this version of Visual Studio.", assembly.AssemblyName.FullName));
                            continue;
                        }
                    }
                    log.LogEntry(3, this.ToString(), string.Format(CultureInfo.CurrentCulture, "Attempting to get Toolbox items from Assembly {0}", new object[] { assembly.AssemblyName.FullName }));
                    if (list2.Contains(assembly.AssemblyName.Name))
                    {
                        ToolboxService.UnloadToolboxItems();
                        list2.Clear();
                    }
                    list2.Add(assembly.AssemblyName.Name);
                    ICollection toolboxItems = ToolboxService.GetToolboxItems(assembly.AssemblyName, true);
                    if (toolboxItems.Count == 0)
                    {
                        log.LogEntry(2, this.ToString(), string.Format(CultureInfo.CurrentCulture, "Assembly {0} did not contain any toolbox items. Please go to documentation for ToolboxService.GetToolboxItems for a list of toolbox item requirements.", new object[] { assembly.AssemblyName.FullName }));
                    }
                    foreach (ToolboxItem item in toolboxItems)
                    {
                        log.LogEntry(3, this.ToString(), string.Format(CultureInfo.CurrentCulture, "Attempting to add Toolbox item {0}", new object[] { item.TypeName }));
                        string defaultCategory = assembly.DefaultCategory;
                        if (assembly.ToolboxItemCategories.ContainsKey(item.TypeName))
                        {
                            defaultCategory = assembly.ToolboxItemCategories[item.TypeName];
                        }
                        if (string.IsNullOrEmpty(defaultCategory))
                        {
                            log.LogEntry(2, this.ToString(), string.Format(CultureInfo.CurrentCulture, "No category set for toolbox item {0}. Consider setting a default category on the assembly registration or create a Categories key to specify per-item categories.", new object[] { item.TypeName }));
                            defaultCategory = "General";
                        }
                        if (string.IsNullOrEmpty(assembly.TargetFramework))
                        {
                            service.AddToolboxItem(item, defaultCategory);
                        }
                        else
                        {
                            IVsToolboxService service2 = service as IVsToolboxService;
                            FrameworkName name = null;
                            try
                            {
                                name = new FrameworkName(assembly.TargetFramework);
                            }
                            catch (ArgumentException)
                            {
                                log.LogEntry(2, this.ToString(), string.Format(CultureInfo.CurrentCulture, "Invalid framework {0} specified for toolbox item {1}. The item will be added without multi-targeting metadata.", new object[] { assembly.TargetFramework, item.TypeName }));
                            }
                            if (name == null)
                            {
                                service.AddToolboxItem(item, defaultCategory);
                            }
                            else
                            {
                                service2.AddToolboxItem(item, defaultCategory, new FrameworkName[] { name }, base.GetType().GUID);
                            }
                        }
                        flag = true;
                        assembly.RegisteredControls.Add(new DotNetControl(item.TypeName, item.DisplayName, item.Description));
                    }
                }
                catch (ArgumentException exception)
                {
                    log.LogEntry(1, this.ToString(), string.Format(CultureInfo.CurrentCulture, "{0}. Did you remember to add this assembly to the GAC or add a string value named CodeBase?", new object[] { exception.Message }));
                }
                catch (FileNotFoundException exception2)
                {
                    log.LogEntry(1, this.ToString(), exception2.Message);
                }
                catch (FileLoadException exception3)
                {
                    log.LogEntry(1, this.ToString(), exception3.Message);
                }
                catch (BadImageFormatException exception4)
                {
                    log.LogEntry(1, this.ToString(), exception4.Message);
                }
            Label_03FF:
                if (flag)
                {
                    list.Add(assembly);
                }
            }
            ToolboxService.UnloadToolboxItems();
            XamlAssembly.UnloadAppDomain();
            return list;
        }

        private void AppendRegistryContents(RegistryKey root, StringBuilder result)
        {
            result.Append(root.Name.Split(new char[] { '\\' }).Last<string>());
            foreach (string str in root.GetValueNames())
            {
                result.Append(str);
                result.Append(root.GetValue(str).ToString());
            }
            foreach (string str2 in root.GetSubKeyNames())
            {
                this.AppendRegistryContents(root.OpenSubKey(str2), result);
            }
        }

        private void ClearCache()
        {
            try
            {
                this.UserRegistryRoot.DeleteSubKeyTree("ToolboxControlsInstallerCache");
            }
            catch (ArgumentException)
            {
            }
        }

        private RegisteredAssembly CreateRegisteredAssembly(RegistryKey key, string keyName)
        {
            if (!string.IsNullOrEmpty(key.GetValue("SilverlightControls") as string))
            {
                return new SilverlightAssembly(new AssemblyName(keyName));
            }
            if (!string.IsNullOrEmpty(key.GetValue("WPFControls") as string))
            {
                return new WPFAssembly(new AssemblyName(keyName));
            }
            return new DotNetAssembly(new AssemblyName(keyName));
        }

        private IEnumerable<AssemblyFolder> GetAllAssemblyFoldersEx()
        {
            Action<string> log = null;
            foreach (string iteratorVariable0 in this.GetInstalledFrameworks())
            {
                foreach (RegistryKey iteratorVariable1 in this.GetAssemblyFoldersExKeys(iteratorVariable0))
                {
                    foreach (string iteratorVariable2 in iteratorVariable1.GetSubKeyNames())
                    {
                        using (RegistryKey iteratorVariable3 = iteratorVariable1.OpenSubKey(iteratorVariable2))
                        {
                            using (RegistryKey iteratorVariable4 = this.HighestServicePack(iteratorVariable3))
                            {
                                using (RegistryKey iteratorVariable5 = iteratorVariable4.OpenSubKey("Toolbox") ?? iteratorVariable3.OpenSubKey("Toolbox"))
                                {
                                    if (iteratorVariable5 != null)
                                    {
                                        byte[] first = this.RegistryTreeHash(iteratorVariable3);
                                        CachedAssemblyFolder cache = CachedAssemblyFolder.GetCache(this.UserRegistryRoot, iteratorVariable3.Name);
                                        if ((cache != null) && first.SequenceEqual<byte>(cache.RegistryHash))
                                        {
                                            yield return cache;
                                        }
                                        else
                                        {
                                            if (log == null)
                                            {
                                                log = delegate (string errMsg) {
                                                    (base.GetService(typeof(SVsActivityLog)) as IVsActivityLog).LogEntry(1, this.ToString(), errMsg);
                                                };
                                            }
                                            NonCachedAssemblyFolder iteratorVariable8 = new NonCachedAssemblyFolder(iteratorVariable0, iteratorVariable4.GetValue("") as string, iteratorVariable5.GetValue("TabName") as string, log);
                                            iteratorVariable8.Cache(this.UserRegistryRoot, iteratorVariable3.Name, first);
                                            yield return iteratorVariable8;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private IEnumerable<RegistryKey> GetAssemblyFoldersExKeys(string tfm)
        {
            return new AssemblyFoldersExKeys(-2) { __this = this, __tfm = tfm };
        }

        private List<RegisteredAssembly> GetCachedAssemblies()
        {
            if (this._cachedAssemblies == null)
            {
                this._cachedAssemblies = new List<RegisteredAssembly>();
                RegistryKey key = this.UserRegistryRoot.OpenSubKey("ToolboxControlsInstallerCache");
                foreach (string str in key.GetSubKeyNames())
                {
                    RegisteredAssembly item = new RegisteredAssembly(new AssemblyName(str));
                    this._cachedAssemblies.Add(item);
                    RegistryKey key2 = key.OpenSubKey(str);
                    item.RegistryHash = key2.GetValue("RegistryHash") as byte[];
                    foreach (string str2 in key2.GetSubKeyNames())
                    {
                        RegistryKey key3 = key2.OpenSubKey(str2);
                        if (key3.GetValue(SilverlightControl.StaticRegDescriptorValue) != null)
                        {
                            item.RegisteredControls.Add(new SilverlightControl(str2, this.GetRegValues(key3)));
                        }
                        else if (key3.GetValue(WPFControl.StaticRegDescriptorValue) != null)
                        {
                            item.RegisteredControls.Add(new WPFControl(str2, this.GetRegValues(key3)));
                        }
                        else
                        {
                            string displayName = key3.GetValue(null).ToString();
                            string description = key3.GetValue("Description").ToString();
                            item.RegisteredControls.Add(new DotNetControl(str2, displayName, description));
                        }
                    }
                }
            }
            return this._cachedAssemblies;
        }

        private IEnumerable<string> GetInstalledFrameworks()
        {
            Array prgSupportedFrameworks = null;
            (base.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SVsFrameworkMultiTargeting)) as Microsoft.VisualStudio.Shell.Interop.IVsFrameworkMultiTargeting).GetSupportedFrameworks(out prgSupportedFrameworks);
            return (from s in (string[]) prgSupportedFrameworks
                where string.IsNullOrEmpty(new FrameworkName(s).Profile)
                select s);
        }

        private List<RegisteredAssembly> GetRegisteredAssemblies()
        {
            if (this._registeredAssemblies == null)
            {
                this._registeredAssemblies = new List<RegisteredAssembly>();
                foreach (RegistryKey key2 in new List<RegistryKey> { this.UserRegistryRoot, this.ApplicationRegistryRoot })
                {
                    RegistryKey key = key2.OpenSubKey("ToolboxControlsInstaller");
                    if (key != null)
                    {
                        foreach (string str in key.GetSubKeyNames())
                        {
                            RegistryKey key3 = key.OpenSubKey(str);
                            RegisteredAssembly item = this.CreateRegisteredAssembly(key3, str);
                            item.AssemblyName.CodeBase = key3.GetValue("Codebase") as string;
                            item.DefaultCategory = key3.GetValue("") as string;
                            item.TargetFramework = key3.GetValue("TargetFramework") as string;
                            item.RegistryHash = this.RegistryTreeHash(key3);
                            RegistryKey key4 = key3.OpenSubKey("ItemCategories");
                            if (key4 != null)
                            {
                                foreach (string str2 in key4.GetValueNames())
                                {
                                    item.ToolboxItemCategories.Add(str2, key4.GetValue(str2).ToString());
                                }
                            }
                            if (!this._registeredAssemblies.Contains(item))
                            {
                                this._registeredAssemblies.Add(item);
                            }
                        }
                    }
                }
                foreach (AssemblyFolder folder in this.GetAllAssemblyFoldersEx())
                {
                    foreach (RegisteredAssembly assembly2 in folder.GetAssemblies())
                    {
                        if (!this._registeredAssemblies.Contains(assembly2))
                        {
                            this._registeredAssemblies.Add(assembly2);
                        }
                    }
                }
            }
            return this._registeredAssemblies;
        }

        private List<RegisteredAssembly> GetRegisteredNotCachedAssemblies()
        {
            List<RegisteredAssembly> cachedAssemblies = this.GetCachedAssemblies();
            return (from registeredAssembly in this.GetRegisteredAssemblies()
                where !cachedAssemblies.Contains(registeredAssembly)
                select registeredAssembly).ToList<RegisteredAssembly>();
        }

        private IDictionary GetRegValues(RegistryKey key)
        {
            IDictionary dictionary = new Hashtable();
            foreach (string str in key.GetValueNames())
            {
                dictionary.Add(str, key.GetValue(str).ToString());
            }
            return dictionary;
        }

        private List<RegisteredAssembly> GetRemovedAssemblies()
        {
            List<RegisteredAssembly> registeredAssemblies = this.GetRegisteredAssemblies();
            return (from cachedAssembly in this.GetCachedAssemblies()
                where !registeredAssemblies.Contains(cachedAssembly)
                select cachedAssembly).ToList<RegisteredAssembly>();
        }

        private IEnumerable<Microsoft.VisualStudio.OLE.Interop.IDataObject> GetToolboxDataObjects(IVsToolbox2 toolboxSvc)
        {
            new List<Microsoft.VisualStudio.OLE.Interop.IDataObject>();
            IEnumToolboxItems pEnum = null;
            toolboxSvc.EnumItems(null, out pEnum);
            Microsoft.VisualStudio.OLE.Interop.IDataObject[] rgelt = new Microsoft.VisualStudio.OLE.Interop.IDataObject[1];
            uint pceltFetched = 0;
            pEnum.Reset();
            while (true)
            {
                if ((pEnum.Next(1, rgelt, out pceltFetched) != 0) || (pceltFetched != 1))
                {
                    yield break;
                }
                yield return rgelt[0];
            }
        }

        private static bool HasReference(Assembly assembly, string referenceName)
        {
            return assembly.GetReferencedAssemblies().Any<AssemblyName>(n => (string.Compare(n.Name, referenceName, StringComparison.OrdinalIgnoreCase) == 0));
        }

        private RegistryKey HighestServicePack(RegistryKey assemblyFolderKey)
        {
            foreach (string str in from s in assemblyFolderKey.GetSubKeyNames()
                orderby s descending
                select s)
            {
                if (str != "Toolbox")
                {
                    RegistryKey key = assemblyFolderKey.OpenSubKey(str);
                    if (!string.IsNullOrEmpty(key.GetValue("") as string))
                    {
                        return key;
                    }
                }
            }
            return assemblyFolderKey;
        }

        int IVsToolboxDataProvider.FileDropped(string fileName, IVsHierarchy pHierSource, out int accepted)
        {
            accepted = 0;
            //using (DefaultUniverse universe = new DefaultUniverse())
            //{
            //    Assembly assembly = universe.LoadAssemblyFromFile(fileName);
            //    RegisteredAssembly assembly2 = null;
            //    if (HasReference(assembly, "PresentationFramework"))
            //    {
            //        assembly2 = new WPFAssembly(assembly.GetName());
            //        IVsTargetFrameworkAssemblies service = base.GetService(typeof(SVsTargetFrameworkAssemblies)) as IVsTargetFrameworkAssemblies;
            //        uint pTargetFrameworkVersion = 0;
            //        service.GetRequiredTargetFrameworkVersionFromDependency(fileName, out pTargetFrameworkVersion);
            //        if (pTargetFrameworkVersion != 0)
            //        {
            //            assembly2.TargetFramework = ".NETFramework,Version=v" + ((pTargetFrameworkVersion >> 0x10)).ToString() + "." + ((pTargetFrameworkVersion & 0xffff)).ToString();
            //        }
            //    }
            //    else if (HasReference(assembly, "System.Windows"))
            //    {
            //        assembly2 = new SilverlightAssembly(assembly.GetName());
            //    }
            //    if (assembly2 != null)
            //    {
            //        List<RegisteredAssembly> assembliesToAddToToolbox = new List<RegisteredAssembly> {
            //            assembly2
            //        };
            //        if (this.AddToolboxItems(assembliesToAddToToolbox).Count > 0)
            //        {
            //            accepted = 1;
            //        }
            //    }
            //}
            return 0;
        }

        int IVsToolboxDataProvider.GetItemInfo(Microsoft.VisualStudio.OLE.Interop.IDataObject pDO, TBXITEMINFO[] pItemInfo)
        {
            return -2147467263;
        }

        int IVsToolboxDataProvider.IsDataSupported(FORMATETC[] fetc, STGMEDIUM[] stg)
        {
            return -2147467263;
        }

        int IVsToolboxDataProvider.IsSupported(Microsoft.VisualStudio.OLE.Interop.IDataObject pDO)
        {
            return -2147467263;
        }

        public void ToolboxInitialize()
        {
            //this.ClearCache();
            this.PackageToolbox_ToolboxUpgraded(null, EventArgs.Empty);
        }

        private void PackageToolbox_ToolboxInitialized(object sender, EventArgs e)
        {
            this.ClearCache();
            this.PackageToolbox_ToolboxUpgraded(sender, e);
        }

        private void PackageToolbox_ToolboxUpgraded(object sender, EventArgs e)
        {
            //if (this.RegistryUpdated())
            //{
                List<RegisteredAssembly> registeredNotCachedAssemblies = this.GetRegisteredNotCachedAssemblies();
                List<RegisteredAssembly> removedAssemblies = this.GetRemovedAssemblies();
                this.RemoveToolboxItems(removedAssemblies);
                List<RegisteredAssembly> installedAssemblies = this.AddToolboxItems(registeredNotCachedAssemblies);
                this.UpdateCache(installedAssemblies, removedAssemblies);
            //}
        }

        private byte[] RegistryTreeHash(RegistryKey root)
        {
            StringBuilder result = new StringBuilder();
            this.AppendRegistryContents(root, result);
            return hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(result.ToString()));
        }

        private bool RegistryUpdated()
        {
            byte[] first = new byte[0];
            using (RegistryKey key = this.UserRegistryRoot.OpenSubKey("ToolboxControlsInstallerCache"))
            {
                if (key != null)
                {
                    byte[] buffer2 = key.GetValue("RegistryHash") as byte[];
                    if (buffer2 != null)
                    {
                        first = buffer2;
                    }
                }
            }
            StringBuilder result = new StringBuilder();
            foreach (RegistryKey key2 in new RegistryKey[] { this.ApplicationRegistryRoot, this.UserRegistryRoot })
            {
                using (RegistryKey key3 = key2.OpenSubKey("ToolboxControlsInstaller"))
                {
                    if (key3 != null)
                    {
                        this.AppendRegistryContents(key3, result);
                    }
                }
            }
            foreach (string str in this.GetInstalledFrameworks())
            {
                foreach (RegistryKey key4 in this.GetAssemblyFoldersExKeys(str))
                {
                    this.AppendRegistryContents(key4, result);
                }
            }
            byte[] second = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(result.ToString()));
            if (first.SequenceEqual<byte>(second))
            {
                return false;
            }
            using (RegistryKey key5 = this.UserRegistryRoot.CreateSubKey("ToolboxControlsInstallerCache"))
            {
                key5.SetValue("RegistryHash", second);
            }
            return true;
        }

        private void RemoveToolboxItems(List<RegisteredAssembly> assembliesWithComponentToRemove)
        {
            if (assembliesWithComponentToRemove.Count != 0)
            {
                List<ToolboxItem> list = new List<ToolboxItem>();
                List<Microsoft.VisualStudio.OLE.Interop.IDataObject> list2 = new List<Microsoft.VisualStudio.OLE.Interop.IDataObject>();
                IVsToolbox2 toolboxSvc = base.GetService(typeof(SVsToolbox)) as IVsToolbox2;
                IToolboxService service = (IToolboxService) base.GetService(typeof(IToolboxService));
                if ((service != null) && (toolboxSvc != null))
                {
                    ToolboxItemCollection toolboxItems = null;
                    foreach (RegisteredAssembly assembly in assembliesWithComponentToRemove)
                    {
                        foreach (RegisteredControl control in assembly.RegisteredControls)
                        {
                            DictionaryControl control2 = control as DictionaryControl;
                            if (control2 != null)
                            {
                                foreach (Microsoft.VisualStudio.OLE.Interop.IDataObject obj2 in this.GetToolboxDataObjects(toolboxSvc))
                                {
                                    if (control2.IsEqualTo(obj2))
                                    {
                                        list2.Add(obj2);
                                    }
                                }
                            }
                            else
                            {
                                DotNetControl control3 = control as DotNetControl;
                                if (control3 != null)
                                {
                                    if (toolboxItems == null)
                                    {
                                        toolboxItems = service.GetToolboxItems();
                                    }
                                    foreach (ToolboxItem item in toolboxItems)
                                    {
                                        if (((item.TypeName == control3.TypeName) && (item.DisplayName == control3.DisplayName)) && (item.AssemblyName.FullName == assembly.AssemblyName.FullName))
                                        {
                                            list.Add(item);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    foreach (ToolboxItem item2 in list)
                    {
                        service.RemoveToolboxItem(item2);
                    }
                    foreach (Microsoft.VisualStudio.OLE.Interop.IDataObject obj3 in list2)
                    {
                        toolboxSvc.RemoveItem(obj3);
                    }
                }
            }
        }

        private void UpdateCache(List<RegisteredAssembly> installedAssemblies, List<RegisteredAssembly> uninstalledAssemblies)
        {
            RegistryKey key = this.UserRegistryRoot.CreateSubKey("ToolboxControlsInstallerCache");
            foreach (RegisteredAssembly assembly in uninstalledAssemblies)
            {
                try
                {
                    key.DeleteSubKeyTree(assembly.AssemblyName.FullName);
                }
                catch (ArgumentException)
                {
                }
            }
            foreach (RegisteredAssembly assembly2 in installedAssemblies)
            {
                if (key.OpenSubKey(assembly2.AssemblyName.FullName) != null)
                {
                    throw new ArgumentException("Assembly has to be uninstalled before it can be installed");
                }
                RegistryKey key2 = key.CreateSubKey(assembly2.AssemblyName.FullName);
                key2.SetValue("RegistryHash", assembly2.RegistryHash);
                foreach (RegisteredControl control in assembly2.RegisteredControls)
                {
                    RegistryKey key3 = key2.CreateSubKey(control.TypeName);
                    DictionaryControl control2 = control as DictionaryControl;
                    if (control2 != null)
                    {
                        key3.SetValue("CreationAssemblyName", control2.Properties["CreationAssemblyName"], RegistryValueKind.String);
                        key3.SetValue("CreationTypeName", control2.Properties["CreationTypeName"], RegistryValueKind.String);
                        key3.SetValue("ToolTypeName", control2.Properties["ToolTypeName"], RegistryValueKind.String);
                    }
                    else
                    {
                        DotNetControl control3 = control as DotNetControl;
                        if (control3 != null)
                        {
                            key3.SetValue(null, control3.DisplayName, RegistryValueKind.String);
                            key3.SetValue("description", control3.Description, RegistryValueKind.String);
                        }
                    }
                    if (!string.IsNullOrEmpty(control.RegDescriptorValue))
                    {
                        key3.SetValue(control.RegDescriptorValue, 1, RegistryValueKind.DWord);
                    }
                }
            }
        }


        [CompilerGenerated]
        private sealed class AssemblyFoldersExKeys : IEnumerable<RegistryKey>, IEnumerable, IEnumerator<RegistryKey>, IEnumerator, IDisposable
        {
            private int state;
            private RegistryKey current;
            public string __tfm;
            public ToolboxInstallerPackage __this;
            public RegistryKey[] __wrap33;
            public int __wrap34;
            public string[] __wrap37;
            public int __wrap38;
            public string[] __wrap3a;
            public int __wrap3b;
            private int initialThreadId;
            public RegistryKey afeKey5__2e;
            public RegistryKey fxKey_5__2d;
            public string fxName_5__2c;
            public RegistryKey fxRootKey_5__2b;
            public string fxRootPath_5__28;
            public FrameworkName name_5__27;
            public RegistryKey root_5__2a;
            public RegistryKey[] roots_5__26;
            public RegistryKey subAfeKey_5__31;
            public RegistryKey subkey_5__30;
            public string subkeyName_5__2f;
            public string tfmVersion_5__29;
            public string tfm;

            [DebuggerHidden]
            public AssemblyFoldersExKeys(int state)
            {
                this.state = state;
                this.initialThreadId = Thread.CurrentThread.ManagedThreadId;
            }

            private void m__Finally32()
            {
                this.state = -1;
            }

            private void m__Finally35()
            {
                this.state = 1;
                if (this.fxRootKey_5__2b != null)
                {
                    this.fxRootKey_5__2b.Dispose();
                }
            }

            private void m__Finally36()
            {
                this.state = 2;
            }

            private void m__Finally39()
            {
                this.state = 3;
            }

            private void m__Finally3c()
            {
                this.state = 5;
                if (this.subkey_5__30 != null)
                {
                    this.subkey_5__30.Dispose();
                }
            }

            public bool MoveNext()
            {
                try
                {
                    int num = this.state;
                    if (num != 0)
                    {
                        //if (num == 4)
                        //{
                        //    goto Label_01E3;
                        //}
                        //if (num == 7)
                        //{
                        //    goto Label_027C;
                        //}
                    }
                    else
                    {
                        this.state = -1;
                        this.roots_5__26 = new RegistryKey[] { Registry.CurrentUser, Registry.LocalMachine };
                        this.name_5__27 = new FrameworkName(this.tfm);
                        if (string.Compare(this.name_5__27.Identifier, "silverlight", StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            this.fxRootPath_5__28 = @"Software\Microsoft\Microsoft SDKs\" + this.name_5__27.Identifier;
                        }
                        else
                        {
                            this.fxRootPath_5__28 = @"Software\Microsoft\" + this.name_5__27.Identifier;
                        }
                        this.tfmVersion_5__29 = "v" + this.name_5__27.Version;
                        this.state = 1;
                        this.__wrap33 = this.roots_5__26;
                        this.__wrap34 = 0;
                        while (this.__wrap34 < this.__wrap33.Length)
                        {
                            this.root_5__2a = this.__wrap33[this.__wrap34];
                            this.fxRootKey_5__2b = this.root_5__2a.OpenSubKey(this.fxRootPath_5__28);
                            this.state = 2;
                            if (this.fxRootKey_5__2b != null)
                            {
                                this.state = 3;
                                this.__wrap37 = this.fxRootKey_5__2b.GetSubKeyNames();
                                this.__wrap38 = 0;
                                while (this.__wrap38 < this.__wrap37.Length)
                                {
                                    {
                                        this.fxName_5__2c = this.__wrap37[this.__wrap38];
                                        if ((this.fxName_5__2c.Length < this.tfmVersion_5__29.Length) || (string.Compare(this.fxName_5__2c.Substring(0, this.tfmVersion_5__29.Length), this.tfmVersion_5__29, StringComparison.OrdinalIgnoreCase) != 0))
                                        {
                                            goto Label_02B0;
                                        }
                                        this.fxKey_5__2d = this.fxRootKey_5__2b.OpenSubKey(this.fxName_5__2c);
                                        this.afeKey5__2e = this.fxKey_5__2d.OpenSubKey("AssemblyFoldersEx");
                                        if (this.afeKey5__2e == null)
                                        {
                                            goto Label_01EF;
                                        }
                                        this.current = this.afeKey5__2e;
                                        this.state = 4;
                                        return true;
                                    }

                                Label_01E3:
                                    this.state = 3;
                                    goto Label_02B0;
                                Label_01EF:
                                    this.state = 5;
                                    this.__wrap3a = this.fxKey_5__2d.GetSubKeyNames();
                                    this.__wrap3b = 0;
                                    while (this.__wrap3b < this.__wrap3a.Length)
                                    {
                                        this.subkeyName_5__2f = this.__wrap3a[this.__wrap3b];
                                        this.subkey_5__30 = this.fxKey_5__2d.OpenSubKey(this.subkeyName_5__2f);
                                        this.state = 6;
                                        this.subAfeKey_5__31 = this.subkey_5__30.OpenSubKey("AssemblyFoldersEx");
                                        if (this.subAfeKey_5__31 == null)
                                        {
                                            goto Label_0283;
                                        }
                                        this.current = this.subAfeKey_5__31;
                                        this.state = 7;
                                        return true;
                                    Label_027C:
                                        this.state = 6;
                                    Label_0283:
                                        this.m__Finally3c();
                                        this.__wrap3b++;
                                    }
                                    this.m__Finally39();
                                Label_02B0:
                                    this.__wrap38++;
                                }
                                this.m__Finally36();
                            }
                            this.m__Finally35();
                            this.__wrap34++;
                        }
                        this.m__Finally32();
                    }
                    return false;
                }
                catch
                {
                    return false;
                }

                return true;
            }

            [DebuggerHidden]
            IEnumerator<RegistryKey> IEnumerable<RegistryKey>.GetEnumerator()
            {
                ToolboxInstallerPackage.AssemblyFoldersExKeys d__;
                if ((Thread.CurrentThread.ManagedThreadId == this.initialThreadId) && (this.state == -2))
                {
                    this.state = 0;
                    d__ = this;
                }
                else
                {
                    d__ = new ToolboxInstallerPackage.AssemblyFoldersExKeys(0) {
                        __this = this.__this
                    };
                }
                d__.tfm = this.__tfm;
                return d__;
            }

            [DebuggerHidden]
            IEnumerator IEnumerable.GetEnumerator()
            {
                return null;

                //return System.Collections.Generic.IEnumerable<Microsoft.Win32.RegistryKey>.GetEnumerator();
            }

            [DebuggerHidden]
            void IEnumerator.Reset()
            {
                throw new NotSupportedException();
            }

            void IDisposable.Dispose()
            {
                switch (this.state)
                {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                        switch (this.state)
                        {
                            case 2:
                            case 3:
                            case 4:
                            case 5:
                            case 6:
                            case 7:
                                try
                                {
                                    switch (this.state)
                                    {
                                        case 3:
                                        case 4:
                                        case 5:
                                        case 6:
                                        case 7:
                                            switch (this.state)
                                            {
                                            }
                                            goto Label_00C1;

                                        default:
                                            break;
                                    }
                                    switch (this.state)
                                    {
                                        case 6:
                                        case 7:
                                            try
                                            {
                                            }
                                            finally
                                            {
                                                this.m__Finally3c();
                                            }
                                            break;
                                    }
                                    this.m__Finally39();
                                Label_00C1:
                                    this.m__Finally36();
                                }
                                finally
                                {
                                    this.m__Finally35();
                                }
                                break;
                        }
                        break;

                    default:
                        return;
                }
                this.m__Finally32();
            }

            RegistryKey IEnumerator<RegistryKey>.Current
            {
                [DebuggerHidden]
                get
                {
                    return this.current;
                }
            }

            object IEnumerator.Current
            {
                [DebuggerHidden]
                get
                {
                    return this.current;
                }
            }
        }

    }
}

